package org.varan;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class VaranRCActivity extends Activity {
	
	public final UUID uuid = UUID.fromString("04c6093b-0000-1000-8000-00805f9b34fb");
	private TextView messageTextView;
	private BluetoothSocket socket;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        
        final BluetoothDevice device; 
        if (adapter != null) {
        	device = adapter.getRemoteDevice("00:1D:60:4D:51:F7");
        } else {
        	device = null;
        }
        //adapter.cancelDiscovery();
        
        String message = "Device not found";
        if (device != null) {
        	message = "Device name: " + device.getName();
        }
        messageTextView = (TextView) findViewById(R.id.deviceTextView);
        messageTextView.setText(message);
        
        socket = null;
		try {
			messageTextView.setText("Creating socket");
			Method m;
			m = device.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
			socket = (BluetoothSocket)m.invoke(device, Integer.valueOf(1)); 
			//socket = device.createRfcommSocketToServiceRecord(uuid);
			messageTextView.setText("Connecting socket");
			socket.connect();
			messageTextView.setText("Socket connected");
		} catch (IOException e) {
			messageTextView.setText(e.getMessage());
			e.printStackTrace();
		} catch (SecurityException e) {
			messageTextView.setText(e.getMessage());
		} catch (NoSuchMethodException e) {
			messageTextView.setText(e.getMessage());
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			messageTextView.setText(e.getMessage());
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			messageTextView.setText(e.getMessage());
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			messageTextView.setText(e.getMessage());
			e.printStackTrace();
		}
		
        final Button sendHelloButton = (Button) findViewById(R.id.sendHelloButton);
        sendHelloButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				byte[] buffer = {'h', 'e', 'l', 'l', 'o'};
				sendMessage(buffer);
			}
		});
        
        final Button sendQuitButton = (Button) findViewById(R.id.sendQuitButton);
        sendQuitButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				byte[] buffer = {'q'};
				sendMessage(buffer);
				try {
					socket.close();
				} catch (IOException e) {
					messageTextView.setText("Socket closed");
					e.printStackTrace();
				}
				socket = null;
			}
		});
    }

	private void sendMessage(byte[] buffer) {
		if (socket == null) {
			messageTextView.setText("Socket is null (closed)");
			return;
		}
		try {
			messageTextView.setText("Sending message");
			socket.getOutputStream().write(buffer);
			messageTextView.setText("Message send");
		} catch (IOException e) {
			messageTextView.setText(e.getMessage());
			e.printStackTrace();
		}
	}
}